﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseUI;
using Utility;
using Hxj.Data;
using System.Data;
using System.Text;
using Utility.Model;

namespace MsgCenter.Web.Pages.ApplicationReg
{
    public partial class Option : BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strGuID = ParamsOfRequest.Request("Guid");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.txtRegDate.ToReadOnly();
                //this.BindUser();
                if (strType == "Add")
                {
                    this.linkSave.Visible = true;
                }
                else if (strType == "Edit")
                {
                    this.txtAppCode.ToReadOnly();
                    this.linkSave.Visible = true;
                    this.SetModelValue();
                }
                else if (strType == "Look")
                {
                    this.SetModelValue();
                    this.txtAppCode.ToReadOnly();
                    this.txtLinkAddress.ToReadOnly();
                    this.txtName.ToReadOnly();
                }
            }
        }

        //private void BindUser()
        //{
        //    using (DataTable dtUser = Bll.FireService.GetUserService().GetUserTable(Sys_User._.IsDelete == false))
        //    {
        //        foreach (DataRow dr in dtUser.Rows)
        //        {
        //            listRole.Items.Add(new ListItem(dr["Name"].ToSafeString(), dr["UserID"].ToSafeString()));
        //        }
        //    }
        //}

        private void SetModelValue()
        {
            Sys_AppReg model = BLL.BLLServiceHelper.GetRegService().GetAppModel(strGuID);
            if (model != null)
            {
                txtAppCode.Value = model.AppCode;
                txtName.Value = model.AppName;
                txtRegDate.Value = model.RegDate.DateTimeToyyyy_MM_dd();
                txtLinkAddress.Value = model.LinkAddress; 
            }
        }

        //private string GetAppUser()
        //{
        //    StringBuilder strUserValue = new StringBuilder();
        //    for (int i = 0; i < listRole.Items.Count; i++)
        //    {
        //        if (listRole.Items[i].Selected == true)
        //        {
        //            if (strUserValue.Length == 0)
        //            {
        //                strUserValue.Append(listRole.Items[i].Value);
        //            }
        //            else
        //            {
        //                strUserValue.Append("," + listRole.Items[i].Value);
        //            }
        //        }
        //    }
        //    return strUserValue.ToString();
        //}

        protected void linkSave_Click(object sender, EventArgs e)
        {
            try
            {
                string strSelectUserID = string.Empty;
                //string strSelectUserID = GetAppUser();
                //if (strSelectUserID.IsNullOrEmpty())
                //{
                //    base.ShowAlert("保存数据出错:请给该应用程序分配用户");
                //    return;
                //}
                Sys_AppReg model = BLL.BLLServiceHelper.GetRegService().GetAppModel(strGuID);
                if (model == null)
                {
                    int rcount = BLL.BLLServiceHelper.GetBaseDataService().ToCount<Sys_AppReg>(Sys_AppReg._.AppCode == txtAppCode.Value);
                    if (rcount == 0)
                    {
                        model = new Sys_AppReg();
                        model.CreateUser = base.Account;
                    }
                    else
                    {
                        base.ShowAlert("保存数据出错:已经存在相同的程序编码");
                        return;
                    }
                }
                model.AppCode = txtAppCode.Value;
                model.AppName = txtName.Value;
                model.RegDate = txtRegDate.Value.ToSafeDateTime();
                model.LinkAddress = txtLinkAddress.Value;
                BLL.BLLServiceHelper.GetRegService().Save(model, strSelectUserID);
                base.ShowAlertScript("Close_Reload();");
            }
            catch (Exception ex)
            {
                base.ShowAlert("保存数据出错:" + ex.Message);
            }
        }
    }
}